๐Ÿ‘ฉโ€๐Ÿ’ป 92334. ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ

@Seongukbaek Posted July 04, 2022 ยท Updated July 04, 2022 ยท 2 min read

๋ฌธ์ œ

Programmers - ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ

๐Ÿ“ Logic

// ์œ ์ €๋ณ„ ์‹ ๊ณ ๋‹นํ•œ ํšŸ์ˆ˜ ์ €์žฅ map Map<String, Integer> reportMap = new HashMap<>(); // ์œ ์ €๋ณ„ ์‹ ๊ณ ํ•œ ์œ ์ € ์ €์žฅ map Map<String, ArrayList<String>> userReportMap = new HashMap<>(); // ์œ ์ €๋ณ„ ๋ฉ”์ผ ๋ฐ›์„ ํšŸ์ˆ˜ ์ €์žฅ map Map<String, Integer> mailMap = new HashMap<>();
  • ์œ ์ €๋ณ„๋กœ, ์‹ ๊ณ ๋‹นํ•œ ํšŸ์ˆ˜์™€ ์‹ ๊ณ ํ•œ ์œ ์ € ๋ฆฌ์ŠคํŠธ, ๊ทธ๋ฆฌ๊ณ  ์ •์ง€ ๊ธฐ์ค€์„ ๋„˜์€ ์œ ์ €๋ฅผ ์‹ ๊ณ ํ•œ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” map์„ ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ“„ CODE

์ฝ”๋“œ ๋ณด๊ธฐ/์ ‘๊ธฐ๐Ÿ’ซ
import java.util.*; class Solution { // ์œ ์ €๋ณ„ ์‹ ๊ณ ๋‹นํ•œ ํšŸ์ˆ˜ ์ €์žฅ map Map<String, Integer> reportMap = new HashMap<>(); // ์œ ์ €๋ณ„ ์‹ ๊ณ ํ•œ ์œ ์ € ์ €์žฅ map Map<String, ArrayList<String>> userReportMap = new HashMap<>(); // ์œ ์ €๋ณ„ ๋ฉ”์ผ ๋ฐ›์„ ํšŸ์ˆ˜ ์ €์žฅ map Map<String, Integer> mailMap = new HashMap<>(); public int[] solution(String[] id_list, String[] report, int k) { for (String rep : report) { String[] line = rep.split(" "); String id = line[0]; String bannedId = line[1]; ArrayList<String> list = null; // ์œ ์ €๋ณ„ ์‹ ๊ณ ํ•œ ์œ ์ € ์ €์žฅ if (userReportMap.containsKey(id)) { list = userReportMap.get(id); if (!list.contains(bannedId)) { list.add(bannedId); reportMap.put(bannedId, reportMap.getOrDefault(bannedId, 0) + 1); } } else { list = new ArrayList<>(); list.add(bannedId); reportMap.put(bannedId, reportMap.getOrDefault(bannedId, 0) + 1); } userReportMap.put(id, list); } // ์œ ์ €๋ณ„ ์‹ ๊ณ ํ•œ ์œ ์ €๊ฐ€ ์ •์ง€ ๊ธฐ์ค€์„ ๋„˜์€ ํšŸ์ˆ˜๋ฅผ mailMap์— ์ €์žฅ for (Map.Entry<String, ArrayList<String>> entry : userReportMap.entrySet()) { ArrayList<String> list = entry.getValue(); for (String id : list) if (reportMap.get(id) >= k) mailMap.put(entry.getKey(), mailMap.getOrDefault(entry.getKey(), 0) + 1); } int[] answer = new int[id_list.length]; for (int i = 0; i < id_list.length; i++) answer[i] = mailMap.getOrDefault(id_list[i], 0); return answer; } }

โœ๏ธ Review

  • ๊ฐ„๋‹จํ•œ ๊ตฌํ˜„ ๋ฌธ์ œ์˜€์œผ๋‚˜, ๋น„ํšจ์œจ์ ์œผ๋กœ ๊ตฌํ˜„ํ•œ ๊ฒƒ ๊ฐ™๋‹ค.
@Seongukbaek
๋ฐฐ์šฐ๊ณ  ์ •๋ฆฌํ•˜๊ณ  ๊ณต์œ ํ•˜๋Š” ๋Œ€ํ•™์ƒ ๋ฐฑ์„ฑ์šฑ์ž…๋‹ˆ๋‹ค ๐Ÿ˜€